package com.weim.weim.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.weim.weim.base.ApiResponse;
import com.weim.weim.entity.WxMiniParam;
import com.weim.weim.service.IWxMiniService;
import com.weim.weim.util.WxMiniappUtil;
import org.springframework.stereotype.Service;

@Service
public class WxMiniServiceImpl implements IWxMiniService {
    @Override
    public ApiResponse<JSONObject> login(WxMiniParam wxMiniParam) {
        //1.获取access_token
        String accessToken = WxMiniappUtil.getAccessToken();

        //2.获取获取手机号
        JSONObject jsonobj = WxMiniappUtil.getWxPhone(wxMiniParam.getCode(), accessToken);
        Integer errcode = jsonobj.getInteger("errcode");
        if (errcode != null && errcode == 40001) {
            String accessTokenAgain = WxMiniappUtil.getAccessToken();
            jsonobj = WxMiniappUtil.getWxPhone(wxMiniParam.getCode(), accessTokenAgain);
        }
        return ApiResponse.ok(jsonobj);
    }
}
